<?php

namespace App\Console\Commands\Transfer;

use App\AppContext\Console;
use App\Facade\System\Role;
use App\Models\System\Group;
use App\Models\System\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UserCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'transfer:user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '迁移';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $data = DB::connection('S')->table('common_admin')->where('id', '>', 1)->get();
        app()->singleton("AppContext", Console::class);
        foreach ($data as $datum) {
            User::create([
                'id' => $datum->id,//
                'group_code' => Group::CUSTOM_SERVICE,
                'role_id' => Role::default(Group::CUSTOM_SERVICE),
                'username' => $datum->login_name,//
                'mobile' => $datum->mobile,//
                'email' => $datum->email ?? "",//
                'password' => $datum->password,//
                'deleted_at' => $datum->status == 1 ? null : date('Y-m-d H:i:s'),
                'salt' => $datum->salt,//
                'hospital_ids' => implode(',', DB::connection('S')
                    ->table('bs_user_hospital')
                    ->where('user_id', '=', $datum->id)
                    ->pluck('hospital_id')->toArray()),
                'department_id' => 0,//
                'name' => $datum->name//
            ]);
        }
    }
}